package com.generator.storageManage.inboundManage.controller;


import com.generator.aboutUser.user.pojo.Users;
import com.generator.storageManage.inboundManage.pojo.*;
import com.generator.storageManage.inboundManage.service.impl.InboundManageService;
import com.generator.util.DateFormatUtil;
import com.generator.util.PageModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/inboundManage")
public class InboundManageController {

    @Autowired
    private InboundManageService inboundManageService;

    public InboundManageService getInboundManageService() {
        return inboundManageService;
    }

    public void setInboundManageService(InboundManageService inboundManageService) {
        this.inboundManageService = inboundManageService;
    }

    @RequestMapping("/queryInboundInfo")
    @ResponseBody
    //获取所有采购单的入库信息
    public PageModel<InboundInfo> queryInboundInfo(InboundInfoCondition condition, HttpServletRequest request, PageModel<InboundInfo> pageModel){
        String purchaseid = request.getParameter("purchaseid");
        String warehousename = request.getParameter("warehousename");
        String status = request.getParameter("status");
        String pageNum = request.getParameter("pageNum");
        Users user = (Users) request.getSession().getAttribute("user");
        System.out.println(user);
        if (pageNum!=null){
            int i = Integer.parseInt(pageNum);
            pageModel.setPageNum(i);
        }

        condition.setPurchaseid(purchaseid);
        condition.setWarehousename(warehousename);
        condition.setStatus(status);
        PageModel<InboundInfo> inboundInfoPageModel = inboundManageService.queryInboundInfo(condition, pageModel);
        if (inboundInfoPageModel!=null&&!inboundInfoPageModel.getData().isEmpty()){
            return inboundInfoPageModel;
        }
        return null;
    }


    @RequestMapping("/updateCgStatus")
    @ResponseBody
    //改变入库状态
    public void updateCgStatus(InboundInfoCondition condition, HttpServletRequest request, HttpServletResponse response) {
        String status = request.getParameter("status");
        String purchaseid = request.getParameter("purchaseid");


        condition.setStatus(status);
        condition.setPurchaseid(purchaseid);
        inboundManageService.updateCgStatus(condition);
    }

    @RequestMapping("/queryPurchaseById")
    @ResponseBody
    //通过采购编号查询采购单信息
    public Purchase queryPurchaseById(HttpServletRequest request,Purchase purchase,HttpServletResponse response) throws IOException {
        String purchaseid = request.getParameter("purchaseid");
        if (purchaseid==null){
            String purchaseid1 = (String)request.getSession().getAttribute("purchaseid");
            Purchase pur = new Purchase();
            pur.setPurchaseid(purchaseid1);
            return inboundManageService.queryPurchaseById(pur);
        }else{
            request.getSession().setAttribute("purchaseid",purchaseid);

            purchase.setPurchaseid(purchaseid);

            return inboundManageService.queryPurchaseById(purchase);
        }
    }


    //查询所有仓库名
    @RequestMapping("/queryAllWareHouse")
    @ResponseBody
    public List<WareHouse> queryAllWareHouse(){
        return inboundManageService.queryAllWareHouse();
    }

    //添加入库记录
    @RequestMapping("inbound")
    @ResponseBody
    public Integer inbound(Inbound inbound, HttpServletRequest request){
        String purchaseid = request.getParameter("purchaseid");
        String wasehousename = request.getParameter("wasehousename");
        String time = request.getParameter("inboundTime");
        Date inboundTime = DateFormatUtil.stringParseToDate(time);
        String username = request.getParameter("username");
        String instatus = request.getParameter("instatus");

        inbound.setInboundTime(inboundTime);
        inbound.setPurchaseid(purchaseid);
        inbound.setWasehousename(wasehousename);
        inbound.setUsername(username);
        inbound.setInstatus(instatus);

        Integer i = inboundManageService.inbound(inbound);
        return i;
    }
}
